package com.qing.gui.frame;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 事件监听
 */
public class TestButtonEventListen {
    public static void main(String[] args) {
        MyFrame myFrame = new MyFrame("事件监听", 200, 200, 500, 500, true, Color.PINK);
        Button bt1 = new Button("bt1");
        //给按钮设置值，事件监听中e.getActionCommand();可以获取到此值，如不设置，获取到的是按钮上的值
        // bt1.setActionCommand("嗯嗯嗯嗯嗯");
        myFrame.add(bt1);
        //给按钮添加事件监听
        MyButtonListen myButtonListen = new MyButtonListen();
        bt1.addActionListener(myButtonListen);
    }

    /**
     * 实现事件触发动作
     */
    static class MyButtonListen implements ActionListener {
        int i = 1;

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("点了" + i + "下按钮");
            i++;
            //获取按钮设置的值
            System.out.println(e.getActionCommand());
        }
    }
}
